//
//  ShopManagerBottomView.m
//  KingSelected
//
//  Created by 杨鹏 on 2023/11/8.
//

#import "ShopManagerBottomView.h"

@interface ShopManagerBottomView ()

@property (nonatomic, strong) UIButton *selectedBtn;
/// 删除
@property (nonatomic, strong) UIButton *deleteBtn;
/// 下架
@property (nonatomic, strong) UIButton *downBtn;
/// 置顶
@property (nonatomic, strong) UIButton *topBtn;
/// 售罄
@property (nonatomic, strong) UIButton *shouqingBtn;

@end

@implementation ShopManagerBottomView

- (instancetype)initWithFrame:(CGRect)frame withType:(NSInteger)type {
    if (self = [super initWithFrame:frame]) {
        [self setupViews:type];
    }
    return self;
}

- (void)setupViews:(NSInteger)type {
    self.backgroundColor = [UIColor whiteColor];
    [self addSubview:self.selectedBtn];
    
    [self.selectedBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(16);
        make.centerY.mas_equalTo(0);
        make.size.mas_equalTo(CGSizeMake(52, 20));
    }];
    [self addSubview:self.deleteBtn];
    
    [self.deleteBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(CGSizeMake(64, 24));
        make.trailing.mas_equalTo(-16);
        make.centerY.mas_equalTo(0);
    }];
    if (type != -1) {
        [self addSubview:self.downBtn];
        [self.downBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.width.height.centerY.equalTo(self.deleteBtn);
            make.trailing.mas_equalTo(self.deleteBtn.mas_leading).offset(-10);
        }];
    }
    if (type == 1) {
        [self addSubview:self.topBtn];
//        [self addSubview:self.shouqingBtn];
        [self.topBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.trailing.mas_equalTo(self.downBtn.mas_leading).offset(-10);
            make.width.height.centerY.equalTo(self.downBtn);
        }];
//        [self.shouqingBtn mas_makeConstraints:^(MASConstraintMaker *make) {
//            make.trailing.mas_equalTo(self.topBtn.mas_leading).offset(-10);
//            make.width.height.centerY.equalTo(self.topBtn);
//        }];
    }
    
    [self.downBtn setTitle:type == 1 ? @"下架" : @"上架" forState:UIControlStateNormal];
}

- (UIButton *)selectedBtn {
    if (!_selectedBtn) {
        _selectedBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_selectedBtn setImage:[UIImage imageNamed:@"icon_selec"] forState:UIControlStateNormal];
        [_selectedBtn setImage:[UIImage imageNamed:@"icon_selec_s"] forState:UIControlStateSelected];
        [_selectedBtn setTitle:@"全选" forState:UIControlStateNormal];
        [_selectedBtn setTitleColor:kRGB(0x999999) forState:UIControlStateNormal];
        [_selectedBtn setTitleColor:kBgColor forState:UIControlStateSelected];
        _selectedBtn.titleLabel.font = kFontRegula(14);
        [_selectedBtn setImageEdgeInsets:UIEdgeInsetsMake(0, 0, 0, 8)];
        @weakify(self)
        [[_selectedBtn rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(id x) {
            @strongify(self)
            self.selectedBtn.selected = !self.selectedBtn.selected;
            if (self.selectedAllBlock) {
                self.selectedAllBlock(self.selectedBtn.selected);
            }
        }];
    }
    return _selectedBtn;
}

- (UIButton *)shouqingBtn {
    if (!_shouqingBtn) {
        _shouqingBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_shouqingBtn setTitle:@"售罄" forState:UIControlStateNormal];
        [_shouqingBtn setTitleColor:kBgColor forState:UIControlStateNormal];
        _shouqingBtn.titleLabel.font = kFontMediium(12);
        _shouqingBtn.backgroundColor = kRGB_ALPHA(0xFF9754, 0.2);
        _shouqingBtn.layer.cornerRadius = 12;
        _shouqingBtn.layer.masksToBounds = YES;
        @weakify(self)
        [[_shouqingBtn rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(id x) {
            @strongify(self)
            if (self.shouqingBlock) {
                self.shouqingBlock();
            }
        }];
    }
    return _shouqingBtn;
}

- (UIButton *)topBtn {
    if (!_topBtn) {
        _topBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        _topBtn.backgroundColor = kBgColor;
        [_topBtn setTitle:@"置顶" forState:UIControlStateNormal];
        [_topBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        _topBtn.titleLabel.font = kFontMediium(12);
        _topBtn.layer.cornerRadius = 12;
        _topBtn.layer.masksToBounds = YES;
        @weakify(self)
        [[_topBtn rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(id x) {
            @strongify(self)
            if (self.topBlock) {
                self.topBlock();
            }
        }];
    }
    return _topBtn;
}

- (UIButton *)downBtn {
    if (!_downBtn) {
        _downBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        _downBtn.backgroundColor = kRGB(0xF2F2F2);
        [_downBtn setTitle:@"下架" forState:UIControlStateNormal];
        [_downBtn setTitleColor:kRGB(0x333333) forState:UIControlStateNormal];
        _downBtn.titleLabel.font = kFontMediium(12);
        _downBtn.layer.cornerRadius = 12;
        _downBtn.layer.masksToBounds = YES;
        @weakify(self)
        [[_downBtn rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(id x) {
            @strongify(self)
            if (self.topDownBlock) {
                self.topDownBlock();
            }
        }];
    }
    return _downBtn;
}

- (UIButton *)deleteBtn {
    if (!_deleteBtn) {
        _deleteBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        _deleteBtn.backgroundColor = [UIColor whiteColor];
        [_deleteBtn setTitle:@"删除" forState:UIControlStateNormal];
        [_deleteBtn setTitleColor:kRGB(0xF15757) forState:UIControlStateNormal];
        _deleteBtn.titleLabel.font = kFontMediium(12);
        _deleteBtn.layer.cornerRadius = 12;
        _deleteBtn.layer.borderColor = kRGB(0xF15757).CGColor;
        _deleteBtn.layer.borderWidth = 1;
        _deleteBtn.layer.masksToBounds = YES;
        @weakify(self)
        [[_deleteBtn rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(id x) {
            @strongify(self)
            if (self.deleteBlock) {
                self.deleteBlock();
            }
        }];
    }
    return _deleteBtn;
}

@end
